class Node
	{
		protected boolean data;
		Node next;
		Node(boolean data, Node next)
		{
			this.data = data;
			this.next = next;
		}
	}


class Linkedlist
{
	protected Node head, tail;
	

     Linkedlist()
	{
    }

	

	public void insert(boolean d)
	{   Node temp;
		if (head == null)
	        {
			temp =new Node(d, null);
		    head =tail=temp;
	        }
		    else
		{
			temp = new Node(d, null);
			tail.next = temp;
			tail = temp;
		}
	}
	public void getList()
	{
		Node n = head;

		if(n == null)
			System.out.println("Empty List");

		while(n != null )
		{
			System.out.print(n.data+"  ");
			n = n.next;
		}
	}

	
	
	public void BinAdd(Linkedlist L1, Linkedlist L2) {
		
		Node N1,N2;
		boolean sum=false, carry=false;
		N1=L1.head;
		N2=L2.head;
		while(N1!=null)
		{
			if ((N1.data==true && N2.data==true)&&(carry==false))
			{
				sum=false;
			    carry=true;
			    this.insert(sum);
			}
			else if ((N1.data==true && N2.data==true)&&(carry==true))
			{
				sum=true;
			    carry=true;
			    this.insert(sum);
			}
			else if ((N1.data==false && N2.data==false)&&(carry==false))
			{
				sum=false;
			    carry=false;
			    this.insert(sum);
			}
			else if ((N1.data==false && N2.data==false)&&(carry==true))
			{
				sum=true;
			    carry=false;
			    this.insert(sum);
			}
			else if (((N1.data==false && N2.data==true)||(N1.data==true && N2.data==false))&&(carry==false))
			{
				sum=true;
			    carry=false;
			    this.insert(sum);
			}
			else if (((N1.data==false && N2.data==true)||(N1.data==true && N2.data==false))&&(carry==true))
			{
				sum=false;
			    carry=true;
			    this.insert(sum);
			}
		
			N1=N1.next;
			N2=N2.next;
		}
		
		//this.getList();
		

	}
}
public class BinaryAdditionDemo{
	public static void main(String[] args)
	{
		Linkedlist l1=new Linkedlist();
		Linkedlist l2=new Linkedlist();
		Linkedlist l3=new Linkedlist();
		
		l1.insert(true);
		l1.insert(true);
		l1.insert(true);
		l1.insert(false);
		System.out.println("\n******  List 1 Data  ******");
		l1.getList();
		l2.insert(true);
		l2.insert(false);
		l2.insert(true);
		l2.insert(false);
		System.out.println("\n******  List 2 Data  ******");
		l2.getList();
		l3.BinAdd(l1,l2);
		System.out.println("\n******  List 3 Data  ******");
		l3.getList();
	}
}

